package com.whenjun.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ifootbook.util.EmptyUtil;
import com.whenjun.dto.door.IscDoorDTO;
import com.whenjun.dto.door.IscDoorReq;
import com.whenjun.entity.IscCamera;
import com.whenjun.entity.IscDoor;
import com.whenjun.mapper.IscCameraMapper;
import com.whenjun.service.IIscCameraService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.ArtemisPostUtil;
import com.whenjun.util.IPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-03-29
 */
@Service
public class IscCameraServiceImpl extends ServiceImpl<IscCameraMapper, IscCamera> implements IIscCameraService {

    @Autowired
    private ArtemisPostUtil artemisPostUtil;

    @Transactional
    public Integer syncCamera(){
        Integer synNumber = 0;

        JSONObject requestBody = new JSONObject();
        requestBody.put("pageNo", 1);
        requestBody.put("pageSize", 1);

        JSONObject jsonObject = (JSONObject) artemisPostUtil.callPostApi("/api/resource/v2/camera/search", requestBody);

        // 需要同步的总数
        Integer total = jsonObject.getInteger("total");

        LocalDateTime now = LocalDateTime.now();

        if (total == null) {
            return synNumber;
        }
        //删除数据
        //personMapper.deleteAll(); // 2019.10.21 注释，手动添加不存在的人员
        // 分页量
        Integer pageSize = 1000;
        // 页数计算
        Integer pageCount = (total / pageSize) + 1;

        for (int i = 1; i <= pageCount; i++) {
            requestBody.put("pageNo", i);
            requestBody.put("pageSize", pageSize);

            JSONObject json = (JSONObject) artemisPostUtil.callPostApi("/api/resource/v2/camera/search", requestBody);
            JSONArray jsonArray = json.getJSONArray("list");
            List<IscCamera> doorList = JSONObject.parseArray(jsonArray.toJSONString(),
                    IscCamera.class);

            int j = 1;
            for (IscCamera iscCamera : doorList) {
                // 保存
                try {
                    iscCamera.setFalshTime(now);

                    this.getBaseMapper().syncCamera(iscCamera);
                } catch (Exception e) {
                    log.debug(e.getMessage());
                    total = total - 1;
                }
                log.debug("第" + i + "组,第" + j + "个");
                j++;
            }
        }
        log.debug("同步监控的总数： " + jsonObject.getInteger("total"));
        log.debug("同步监控的实际总数： " + total);
        return total;
    }

    @Transactional
    public IPage<IscCamera> KqCameras(IscDoorReq iscDoorReq) {
        Page<IscCamera> page = new Page<>(iscDoorReq.getPage(), iscDoorReq.getSize());

        IPage<IscCamera> iscCameraPage = this.lambdaQuery()
                .like(!EmptyUtil.isEmpty(iscDoorReq.getName()), IscCamera::getName, iscDoorReq.getName())
                .orderByAsc(IscCamera::getName)
                .page(page)
                ;

        IPage<IscCamera> iscCameraIPage = IPageUtil.trans(iscCameraPage);
        return iscCameraIPage;
    }
}
